﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class DialogStyleNo : ICommonForm
    {
        private string imageFileName = "";
        public DialogStyleNo()
        {
            InitializeComponent();
            //Set Top Most
            this.TopMost = Constants.FullScreen();
        }
        ConfigurationMng configurationMng = new ConfigurationMng();
        private int id;

        public string result
        {
            get
            {
                return txtName.Text.Trim() + "*" + imageFileName;
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (result == "")
            {
                MessageBox.Show("Hãy chọn một giá trị!", "Chú ý");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        private void BindDataGrid()
        {
            dataGridView1.DataSource = configurationMng.GetAllItems(Constants.StyleType);
            dataGridView1.ClearSelection();
            txtName.Focus();
            btnAdd.Enabled = true;
            btnUpdate.Enabled = btnDel.Enabled = false;
            txtName.Text = "";
            browseImageDialog = new OpenFileDialog();
            pictureBox1.Image = null;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("Bạn phải nhập mã kiểu dáng.", "Chú ý");
                txtName.Focus();
                return;
            }
            string fileName;
            try
            {
                fileName = string.IsNullOrEmpty(browseImageDialog.SafeFileName) ? "" : browseImageDialog.SafeFileName;
            }
            catch (NullReferenceException)
            {
                fileName = "";
            }
            var newName = txtName.Text.Trim();
            if (configurationMng.AddStyleItem(Constants.StyleType, newName, fileName))
            {
                // Copy image
                if (pictureBox1.Image != null)
                    File.Copy(browseImageDialog.FileName,
                    Application.StartupPath + "\\" + Constants.ImageFilePath + browseImageDialog.SafeFileName,
                    true);
                BindDataGrid();
                SelectAddedRow(newName);
            }
        }
        private void SelectAddedRow(string name)
        {
            btnBoChon_Click(null, null);
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (name == Convert.ToString(row.Cells[0].Value))
                {
                    row.Selected = true;
                    txtName.Focus();
                    dataGridView1_RowHeaderMouseClick(null, null);
                    return;
                }
            }
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var id = "";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    id = Convert.ToString(row.Cells[1].Value);
                }
            }
            if (string.IsNullOrEmpty(id))
            {
                MessageBox.Show(Constants.Msg.ChooseOneRow, "Chú ý");
                return;
            }
            string fileName;
            try
            {
                fileName = string.IsNullOrEmpty(browseImageDialog.SafeFileName) ? "" : browseImageDialog.SafeFileName;
            }
            catch (NullReferenceException)
            {
                fileName = "";
            }
            if (configurationMng.UpdateStyleItem(Convert.ToInt32(id), txtName.Text.Trim(), fileName))
            {
                if (!string.IsNullOrEmpty(browseImageDialog.FileName))
                    File.Copy(browseImageDialog.FileName,
                    Application.StartupPath + "\\" + Constants.ImageFilePath + browseImageDialog.SafeFileName,
                    true);
                BindDataGrid();
                MessageBox.Show("Cập nhật thành công!", "Chú ý");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var id = "";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    id = Convert.ToString(row.Cells[1].Value);
                }
            }
            if (string.IsNullOrEmpty(id))
            {
                MessageBox.Show(Constants.Msg.ChooseOneValue, "Chú ý");
                return;
            }
            if (MessageBox.Show(Constants.Msg.ConfirmDelete, "Chú ý", MessageBoxButtons.YesNo) == DialogResult.Yes)
                if (configurationMng.DeleteItem(Convert.ToInt32(id)))
                {
                    MessageBox.Show("Xóa thành công!", "Chú ý");
                    BindDataGrid();
                }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    txtName.Text = Convert.ToString(row.Cells[0].Value);
                    btnAdd.Enabled = false;
                    btnUpdate.Enabled = btnDel.Enabled = true;
                    var item = configurationMng.GetItemByElements(Convert.ToInt32(row.Cells[1].Value));
                    try
                    {
                        pictureBox1.Image = item.Description != null ? Image.FromFile(Constants.ImageFilePath + item.Description) : null;
                        imageFileName = item.Description;
                    }
                    catch (FileNotFoundException)
                    {
                        pictureBox1.Image = null;
                    }
                }
            }
        }

        private void DialogStyleNo_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            BindDataGrid();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Selected)
                {
                    txtName.Text = Convert.ToString(row.Cells[0].Value);
                    row.Selected = true;
                    btnAdd.Enabled = false;
                    btnUpdate.Enabled = btnDel.Enabled = true;
                    var item = configurationMng.GetItemByElements(Convert.ToInt32(row.Cells[1].Value));
                    try
                    {
                        pictureBox1.Image = item.Description != null ?
                        Image.FromFile(Constants.ImageFilePath + item.Description) :
                        null;
                        imageFileName = item.Description;
                    }
                    catch (FileNotFoundException)
                    {
                        pictureBox1.Image = null;
                    }
                    return;
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            browseImageDialog = new OpenFileDialog();
            browseImageDialog.Title = @"Select product image:";
            browseImageDialog.InitialDirectory = Application.StartupPath + Constants.ImageFilePath;
            browseImageDialog.Filter =
            @"Image files (*.jpg; *.png; *.bmp; *.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif|All files (*.*)|*.*";
            browseImageDialog.FilterIndex = 1;
            browseImageDialog.RestoreDirectory = true;
            if (browseImageDialog.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(browseImageDialog.FileName);
                imageFileName = browseImageDialog.SafeFileName;
            }
        }

        private void btnBoChon_Click(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
            txtName.Focus();
            btnAdd.Enabled = true;
            btnUpdate.Enabled = btnDel.Enabled = false;
            txtName.Text = "";
            browseImageDialog = new OpenFileDialog();
            pictureBox1.Image = null;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1_CellClick(null, null);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
